import { createContext, useState } from "react";
import PatientRegistration from "../views/PatientRegistration";
import PatientLists from "../views/PatientLists";
import Outpatientregistration from "../views/Outpatientregistration";
import Registrationinquiry from "../views/Registrationinquiry";
import Blacklistmanagement from "../views/Blacklistmanagement";
import Doctorscheduling from "../views/Doctorscheduling";
import CopyScheduling from "../views/CopyScheduling";
import SchedulingSettings from "../views/SchedulingSettings";
import Schedulinginquiry from "../views/Schedulinginquiry";
import Patientfilingstatistics from "../views/Patientfilingstatistics";
import Outpatientregistrationstatistics from "../views/Outpatientregistrationstatistics";
import Dictionarymanaged from "../views/Dictionarymanaged";
import Parameterconfiguration from "../views/Parameterconfiguration";
import LoginLog from "../views/LoginLog";
import OperationLog from "../views/OperationLog";
import Indexcontent from "../views/IndexContent";

export const context = createContext<any>({})

// 导航
const Navs = [
    {
        key: '/index/indexcontent',
        icon: <img src="https://cdn7.axureshop.com/demo2024/2321849/images/%E7%B3%BB%E7%BB%9F%E6%A1%86%E6%9E%B6/u1095.svg" alt="" />,
        label: '首页',
        element:<Indexcontent/>
    },
    {
        key: '/index/patientmangent',
        icon: <img src="https://cdn7.axureshop.com/demo2024/2321849/images/%E7%B3%BB%E7%BB%9F%E6%A1%86%E6%9E%B6/u1095.svg" alt="" />,
        label: '患者管理',
        roles:['admin','edior'],
        children: [
            { key: '/index/patientmangent/PatientRegistration', label:'患者登记' , element:<PatientRegistration/> , roles:['admin'],},
            { key: '/index/patientmangent/PatientLists', label: '患者列表' , element:<PatientLists/>, roles:['admin','edior'],},
        ],
    },
    {
        key: '/index/Registrationmanagement',
        icon: <img src="https://cdn7.axureshop.com/demo2024/2321849/images/%E7%B3%BB%E7%BB%9F%E6%A1%86%E6%9E%B6/u1095.svg" alt="" />,
        label: '挂号管理',
        roles:['admin','edior'],
        children: [
            { key: '/index/Registrationmanagement/Outpatientregistration', label: '门诊挂号' , element:<Outpatientregistration/>,roles:['admin','edior'],},
            { key: '/index/Registrationmanagement/Registrationinquiry', label: '挂号查询' , element:<Registrationinquiry/>,roles:['admin','edior'],},
            { key: '/index/Registrationmanagement/Blacklistmanagement', label: '黑名单管理', element:<Blacklistmanagement/> , roles:['admin'],},
        ],
    },
    {
        key: '/index/WorkforceManagement',
        icon: <img src="https://cdn7.axureshop.com/demo2024/2321849/images/%E7%B3%BB%E7%BB%9F%E6%A1%86%E6%9E%B6/u1095.svg" alt="" />,
        label: '排班管理',
        roles:['admin','edior'],
        children: [
            { key: '/index/WorkforceManagement/Doctorscheduling', label: '医生排班', element:<Doctorscheduling/> , roles:['admin','edior'],},
            { key: '/index/WorkforceManagement/CopyScheduling', label: '复制排班', element:<CopyScheduling/> , roles:['admin'],},
            { key: '/index/WorkforceManagement/SchedulingSettings', label: '排班设置', element:<SchedulingSettings/> , roles:['admin'],},
            { key: '/index/WorkforceManagement/Schedulinginquiry', label: '排班查询',element:<Schedulinginquiry/> , roles:['admin'],},
        ],
    },
    {
        key: '/index/statisticsquery',
        icon: <img src="https://cdn7.axureshop.com/demo2024/2321849/images/%E7%B3%BB%E7%BB%9F%E6%A1%86%E6%9E%B6/u1095.svg" alt="" />,
        label: '统计查询',
        roles:['admin','kf'],
        children: [
            { key: '/index/statisticsquery/Patientfilingstatistics', label: '患者建档统计', element:<Patientfilingstatistics/> , roles:['admin','kf'],},
            { key: '/index/statisticsquery/Outpatientregistrationstatistics', label: '门诊挂号统计' , element:<Outpatientregistrationstatistics/> , roles:['admin','kf'],},
        ],
    },
    {
        key: '/index/systemmanagement',
        icon: <img src="https://cdn7.axureshop.com/demo2024/2321849/images/%E7%B3%BB%E7%BB%9F%E6%A1%86%E6%9E%B6/u1095.svg" alt="" />,
        label: '系统管理',
        roles:['admin'],
        children: [
            { key: '/index/systemmanagement/Dictionarymanaged', label: '字典管理' , element:<Dictionarymanaged/> ,roles:['admin'],},
            { key: '/index/systemmanagement/Parameterconfiguration', label: '参数配置' , element:<Parameterconfiguration/>, roles:['admin'], },
            {
                key: '/index/systemmanagement/Logmanagement',
                label: '日志管理',
                children: [
                    { key: '/index/systemmanagement/Logmanagement/LoginLog', label: '登录日志' ,element:<LoginLog/> , roles:['admin'],},
                    { key: '/index/systemmanagement/Logmanagement/OperationLog', label: '操作日志', element:<OperationLog/> , roles:['admin'],}
                ]
            },
        ],
    },
];


// 根据role角色生成侧边导航栏
const findRoles = (role:string) => {
    const arr:any = [];

    const findInfo = (data:any,parent:any = null)=>{
        data.forEach((item:any)=>{
            const { children , ...info} = item
            if(children){
                info.children=[]
                findInfo(children,info.children)
                info.children.length==0 ? delete info.children : null
            }
            if(info.roles){
                if(info.roles?.includes(role)){
                    parent ? parent.push(info) : arr.push(info)
                }
            }else{
                parent ? parent.push(info) : arr.push(info)
            }
        })
    }
    findInfo(Navs);
    return arr
}

// 根据侧边栏实现路由的扁平化处理
const flatRoutes = (menus:any) =>{
    const arr:any = []
    const findInfo = (data:any) => {
        data.forEach((item:any)=>{
            const { children , ...info} = item
            arr.push(info)
            if(children){
                findInfo(children)
            }
        })
    }
    findInfo(menus)
    return arr
}

function AppProvider({ children }:any) {
    // 初始化的时候从本地存储获取角色信息
    let defaultMenus = []
    let defaultRoutes = []
    const oldRole = sessionStorage.getItem('role')
    if(oldRole){
        defaultMenus = findRoles(oldRole)
        defaultRoutes = flatRoutes(defaultMenus)
    }
    const [meaus,setMeaus] = useState(defaultMenus)
    const [routes,setRoutes] = useState(defaultRoutes)
    // 根据当前角色生成路由数据和侧边栏数组
    
    const resetMenus = (role:string) => {
        sessionStorage.setItem('role',role)
        // 此处重置菜单和路由数据
        const tmpMeun = findRoles(role)
        setMeaus(tmpMeun)
        
        setRoutes(flatRoutes(tmpMeun))
    }
    return <context.Provider value={{ meaus , routes , resetMenus ,}}>{children}</context.Provider>
}



export default AppProvider;